package com.zybzzz.workapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.zybzzz.workapp.entry.PlanDate;
import com.zybzzz.workapp.fragment.PlanDateFragment;
import com.zybzzz.workapp.utils.DateChangeUtils;

import org.joda.time.DateTime;

import java.util.Date;
import java.util.List;

import noman.weekcalendar.WeekCalendar;
import noman.weekcalendar.listener.OnDateClickListener;

public class WeekActivity extends AppCompatActivity {

    private WeekCalendar weekCalendar;
    private Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weekact);

        date=new Date();
        init();
        //生成放计划文本的容器
//        FragmentManager fm=getSupportFragmentManager();
//        Fragment fragment=fm.findFragmentById(R.id.calendar_plan_layout_container);
//
//
//        if (fragment==null){
//            fragment=new PlanDateFragment();
//            fm.beginTransaction().add(R.id.calendar_plan_layout_container,fragment).commit();
//        }

//        removeFragment();
        makeFragment();


    }

//    private void removeFragment(){
//        FragmentManager fm=getSupportFragmentManager();
//        Fragment fragment=fm.findFragmentById(R.id.calendar_plan_layout_container);

//        if (fragment!=null){
//            fragment
//        }

//    }

    private void makeFragment(){

        FragmentManager fm=getSupportFragmentManager();
        Fragment fragment=fm.findFragmentById(R.id.calendar_plan_layout_container);



        if (fragment==null){
            fragment=PlanDateFragment.makeNewInstance(DateChangeUtils.date2String(this.date));
            fm.beginTransaction().add(R.id.calendar_plan_layout_container,fragment).commit();
        }else {
            fragment=PlanDateFragment.makeNewInstance(DateChangeUtils.date2String(this.date));
            fm.beginTransaction().replace(R.id.calendar_plan_layout_container,fragment).commit();
        }
    }

    private void init() {
//        Button todaysDate = (Button) findViewById(R.id.today);
//        Button selectedDate = (Button) findViewById(R.id.selectedDateButton);
//        Button startDate = (Button) findViewById(R.id.startDate);
//        todaysDate.setText(new DateTime().toLocalDate().toString() + " (Reset Button)");
//        selectedDate.setText(new DateTime().plusDays(50).toLocalDate().toString()
//                + " (Set Selected Date Button)");
//        startDate.setText(new DateTime().plusDays(7).toLocalDate().toString()
//                + " (Set Start Date Button)");

        weekCalendar = (WeekCalendar) findViewById(R.id.weekCalendar);
        weekCalendar.setOnDateClickListener(new OnDateClickListener() {
            @Override
            public void onDateClick(DateTime dateTime) {
                Toast.makeText(WeekActivity.this, "You Selected " + dateTime.toString(), Toast
                        .LENGTH_SHORT).show();
            }

        });
//        weekCalendar.setOnWeekChangeListener(new OnWeekChangeListener() {
//            @Override
//            public void onWeekChange(DateTime firstDayOfTheWeek, boolean forward) {
//                Toast.makeText(WeekActivity.this, "Week changed: " + firstDayOfTheWeek +
//                        " Forward: " + forward, Toast.LENGTH_SHORT).show();
//            }
//        });
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        MenuInflater inflater = getMenuInflater();
//        inflater.inflate(R.menu.menu, menu);
//        return super.onCreateOptionsMenu(menu);
//
//    }
//
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//        if(item.getItemId() == R.id.action_github){
//            openGithubRepo();
//        }
//        return true;
//    }
//
//    private void openGithubRepo() {
//        Intent intent = new Intent(Intent.ACTION_VIEW);
//        intent.setData(Uri.parse("https://github.com/nomanr/WeekCalendar"));
//        startActivity(intent);
//    }

    public void onNextClick(View view) {
        weekCalendar.moveToNext();
        this.date=DateChangeUtils.nextDate(this.date);
//        removeFragment();
        makeFragment();
    }


    public void onPreviousClick(View view) {
        weekCalendar.moveToPrevious();
        this.date=DateChangeUtils.prevDate(this.date);
//        removeFragment();
        makeFragment();
    }


//    public void onResetClick(View view) {
//        weekCalendar.reset();
//
//    }

    //暂时没用 按钮将要被删除
//    public void onSelectedDateClick(View view){
//        weekCalendar.setSelectedDate(new DateTime().plusDays(50));
//    }
//    public void onStartDateClick(View view){
//        weekCalendar.setStartDate(new DateTime().plusDays(7));
//    }
}
